package com.xk.app_home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils

import com.example.common.OnClickItem


import com.xk.app_home.R
import com.xk.app_home.data.Article

class ArticleAdapter(var data: Article, private var onclick: OnClickItem) : RecyclerView.Adapter<ArticleAdapter.MViewHolder>(){


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MViewHolder {
        val view =   LayoutInflater.from(parent.context).inflate(R.layout.layout, parent, false)
        return MViewHolder(view)
    }

    override fun getItemCount(): Int {
        return data.datas.size
    }

    inner class MViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tv_title :TextView = itemView.findViewById(R.id.tv_title)
        var tv_fresh :TextView = itemView.findViewById(R.id.tv_fresh)
        var iv_favorite : ImageView = itemView.findViewById(R.id.iv_favorite)
        var tv_category : TextView = itemView.findViewById(R.id.tv_category)
        var tv_time : TextView = itemView.findViewById(R.id.tv_time)
        var tv_author :TextView = itemView.findViewById(R.id.tv_author)
        var item :ConstraintLayout = itemView.findViewById(R.id.item)
    }

    override fun onBindViewHolder(holder: MViewHolder, position: Int) {
        LogUtils.d(position)
        holder.item.setOnClickListener {
           onclick.onclickItem(data.datas.get(position).link)
        }
        if(data.datas.get(position).fresh){
            holder.tv_fresh.visibility = View.VISIBLE
        }else{
            holder.tv_fresh.visibility = View.GONE
        }
        if(data.datas[position].superChapterId == 1){
            holder.iv_favorite.setImageResource(R.drawable.ic_baseline_favorite_24)
        }else{
            holder.iv_favorite.setImageResource(R.drawable.ic_baseline_favorite_24_black)
        }
        holder.tv_title.text = data.datas.get(position).title
        holder.tv_category.text = data.datas.get(position).superChapterName
        if(data.datas.get(position).collect){
            holder.iv_favorite.visibility = View.VISIBLE
        }else{
            holder.iv_favorite.visibility = View.GONE
        }
        holder.tv_time.setText(data.datas.get(position).niceDate)
        holder.tv_author.text = data.datas.get(position).shareUser

    }

}
